<blockquote>
    <p>Criteria插件</p>
    <footer>
        <p>通过Db实例直接获取Criteria对象</p>
    </footer>
</blockquote>
<blockquote>
    <p>示例 Entity</p>
    <footer style="color:red;">和数据库字段和数据表产生映射关系</footer>
</blockquote>
<pre class="code brush:java">
package cn.jants.auth.entity;


import com.alibaba.fastjson.annotation.JSONField;
import cn.jants.common.annotation.action.Entity;
import cn.jants.plugin.orm.Column;
import cn.jants.plugin.orm.Id;
import cn.jants.plugin.orm.Table;

import java.io.Serializable;

/**
 * 用户管理(sys_user)
 *
 * @author MrShun
 * @version 1.0.0 2017-09-11
 */
@Table(name = "sys_user")
public class User implements Serializable {

    /** @Id标识主键 */
    @Id
    @Column(name = "id")
    private Long id;

    /**
     * 登陆帐户
     */
    @Column(name = "account")
    private String account;

    /**
     * 密码
     */
    @Column(name = "password")
    private String password;

    /**
     * 用户类型(1普通用户2管理员3系统管理员)
     */
    @Column(name = "user_type")
    private Integer userType;

    /**
     * 姓名
     */
    @Column(name = "user_name")
    private String userName;

    /**
     * 姓名拼音
     */
    @Column(name = "name_pinyin")
    private String namePinyin;

    /**
     * 性别(0:未知;1:男;2:女)
     */
    @Column(name = "sex")
    private Integer sex;

    /**
     * 头像
     */
    @Column(name = "avatar")
    private String avatar;

    /**
     * 电话
     */
    @Column(name = "phone")
    private String phone;

    /**
     * 邮箱
     */
    @Column(name = "email")
    private String email;

    /**
     * 微信
     */
    @Column(name = "weixin")
    private String weixin;

    /**
     * 微博
     */
    @Column(name = "weibo")
    private String weibo;

    /**
     * QQ
     */
    @Column(name = "qq")
    private String qq;

    /**
     * 关联公众号openid
     */
    @Column(name = "openid")
    private String openid;

    /**
     * 职位
     */
    @Column(name = "position")
    private String position;

    /**
     * 登录次数
     */
    @Column(name = "login_count")
    private Long loginCount;

    /**
     * 登录IP
     */
    @Column(name = "login_ip")
    private String loginIp;

    /**
     * 最后登录时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm")
    @Column(name = "last_login_time")
    private java.util.Date lastLoginTime;

    /**
     * 账号状态 0、正常 1、锁定
     */
    @Column(name = "is_lock")
    private Integer isLock;

    /**  */
    @Column(name = "remark")
    private String remark;

    /**
     * 创建时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm")
    @Column(name = "create_time")
    private java.util.Date createTime;

    /**
     * 创建人
     */
    @Column(name = "create_by")
    private String createBy;

    /**
     * 修改时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm")
    @Column(name = "update_time")
    private java.util.Date updateTime;

    /**
     * 修改人
     */
    @Column(name = "update_by")
    private String updateBy;

    /**
     * 是否已删除(0未删除1已删除)
     */
    @Column(name = "is_delete")
    private Integer isDelete;


    /**
     * 角色列表ID ","隔开
     */
    private String roles;

    /**
     * 组织列表ID ","隔开
     */
    private String orgs;

    public User() {
    }

    public User(Long id) {
        this.id = id;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }


    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }


    public String getNamePinyin() {
        return namePinyin;
    }

    public void setNamePinyin(String namePinyin) {
        this.namePinyin = namePinyin;
    }


    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }


    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }


    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public String getWeixin() {
        return weixin;
    }

    public void setWeixin(String weixin) {
        this.weixin = weixin;
    }


    public String getWeibo() {
        return weibo;
    }

    public void setWeibo(String weibo) {
        this.weibo = weibo;
    }


    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }


    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }


    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }


    public Long getLoginCount() {
        return loginCount;
    }

    public void setLoginCount(Long loginCount) {
        this.loginCount = loginCount;
    }


    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }


    public java.util.Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(java.util.Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }


    public Integer getIsLock() {
        return isLock;
    }

    public void setIsLock(Integer isLock) {
        this.isLock = isLock;
    }


    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }


    public java.util.Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(java.util.Date createTime) {
        this.createTime = createTime;
    }


    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }


    public java.util.Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(java.util.Date updateTime) {
        this.updateTime = updateTime;
    }


    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }


    public Integer getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(Integer isDelete) {
        this.isDelete = isDelete;
    }

    public String getRoles() {
        return roles;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }

    public String getOrgs() {
        return orgs;
    }

    public void setOrgs(String orgs) {
        this.orgs = orgs;
    }
}
</pre>
<blockquote>
    <p>示例 UserService业务类</p>
</blockquote>
<pre class="code brush:java">
package cn.jants.auth.service;

import cn.jants.auth.entity.User;
import cn.jants.auth.entity.UserOrg;
import cn.jants.auth.entity.UserRole;
import cn.jants.auth.generate.*;
import cn.jants.common.annotation.service.Service;
import cn.jants.common.annotation.service.Source;
import cn.jants.common.annotation.service.Tx;
import cn.jants.common.bean.Log;
import cn.jants.common.bean.Page;
import cn.jants.common.exception.TipException;
import cn.jants.common.utils.FileUtil;
import cn.jants.common.utils.RegexUtil;
import cn.jants.common.utils.StrEncryptUtil;
import cn.jants.common.utils.StrUtil;
import cn.jants.plugin.db.Db;
import cn.jants.plugin.orm.Criteria;
import cn.jants.plugin.orm.enums.Condition;
import cn.jants.plugin.orm.enums.OrderBy;
import cn.jants.plugin.orm.enums.Relation;

import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-06-19
 */
@Service
public class UserService {

    @Source
    private Db db;

    public Page queryPage(Integer pageIndex, Integer pageSize, String sortField, String sortOrder, String filters, String tjKey, String keyValue) {
        try {
            Criteria criteria = db.createCriteria(User.class);
            if (filters != null) {
                criteria.filters(filters);
            }
            criteria.orderBy(OrderBy.valueOf(sortOrder), sortField);
            if (StrUtil.notBlank(tjKey, keyValue)) {
                criteria.and(tjKey, Condition.LIKE, "%".concat(keyValue).concat("%"));
            }
            return criteria.findPage(pageIndex, pageSize);
        } catch (Exception e) {
            Log.error("conditional conversion error:{}", filters);
            return null;
        }
    }

    /**
     * 根据用户ID查询用户信息包含角色，组织ID
     *
     * @param id 用户Id
     * @return
     */

    public User find(Long id) {
        Criteria&lt;User> criteria = db.createCriteria(User.class);
        User user = criteria.findById(id);
        //填充用户角色信息
        Criteria urCriteria = db.createCriteria(UserRole.class);
        urCriteria.and(QUserRole.USER_ID, Condition.EQ, id);
        List&lt;UserRole> userRoles = urCriteria.findList();
        String rolesStr = "", orgsStr = "";
        if (userRoles != null && userRoles.size() != 0) {
            int len = userRoles.size();
            for (int i = 0; i < len; i++) {
                rolesStr += userRoles.get(i).getRoleId();
                if (i != len - 1) {
                    rolesStr += ",";
                }
            }

        }
        user.setRoles(rolesStr);

        //查询填充用户组织信息
        Criteria uoCriteria = db.createCriteria(UserOrg.class);
        uoCriteria.and(QUserOrg.USER_ID, Condition.EQ, id);
        List&lt;UserOrg> userOrgs = uoCriteria.findList();
        if (userOrgs != null && userOrgs.size() != 0) {
            int len = userOrgs.size();
            for (int i = 0; i < len; i++) {
                orgsStr += userOrgs.get(i).getOrgId();
                if (i != len - 1) {
                    orgsStr += ",";
                }
            }

        }
        user.setOrgs(orgsStr);
        return user;
    }

    /**
     * 根据用户ID查询角色或组织名称信息
     *
     * @param uid  用户ID
     * @param type 查询条件 1/查询角色2/查询组织/全部查询
     */
    public List findRolesOrOrgs(Long uid, int type) {
        if (uid == null) {
            throw new TipException("用户ID参数不能为空!");
        }
        if (type == 1) {
            Criteria criteria = db.createCriteria(UserRole.class);
            criteria.and(QUserRole.USER_ID, Condition.EQ, uid);
            criteria.label(QRole._ID, QRole._ROLE_NAME);
            criteria.addRelation(Relation.lEFT, QRole.TABLE, QUserRole.ROLE_ID, QRole._ID);
            return criteria.findList();
        } else if (type == 2) {
            Criteria criteria = db.createCriteria(UserOrg.class);
            criteria.and(QUserOrg.USER_ID, Condition.EQ, uid);
            criteria.label(QOrg._ID, QOrg._ORG_NAME);
            criteria.addRelation(Relation.lEFT, QOrg.TABLE, QUserOrg.ORG_ID, QOrg._ID);
            return criteria.findList();
        }
        return null;
    }

    @Tx
    public int save(User user) {
        String account = user.getAccount();
        if (account == null && "".equals(account.trim())) {
            throw new TipException("用户账号不能为空!");
        }
        if (!RegexUtil.isENG_NUM_(account)) {
            throw new TipException("用户必须是英文字母+数字+下划线组成!");
        }
        Criteria criteria = db.createCriteria(User.class);
        criteria.and(QUser.ACCOUNT, Condition.EQ, account);
        Integer count = criteria.count();
        if (count > 0) {
            return -1;
        }
        user.setLoginCount(0L);
        user.setIsLock(0);
        user.setPassword(StrEncryptUtil.md5(account));
        user.setCreateTime(new Date());
        user.setIsDelete(0);
        //存储用户
        Long uid = criteria.saveReturnKey(user);
        //先清空用户对应角色, 在存储用户角色
        String roles = user.getRoles();
        saveUserRoles(uid, roles);

        //先清空用户对应组织, 在存储用户组织
        String orgs = user.getOrgs();
        saveUserOrgs(uid, orgs);
        return 1;
    }

    @Tx
    public int update(User user) {
        Long uid = user.getId();
        if (uid == null) {
            throw new TipException("用户ID不能为空!");
        }
        String account = user.getAccount();
        if (account == null && "".equals(account.trim())) {
            throw new TipException("用户账号不能为空!");
        }
        if (!RegexUtil.isENG_NUM_(account)) {
            throw new TipException("用户必须是英文字母+数字+下划线组成!");
        }
        Criteria criteria = db.createCriteria(User.class);
        criteria.and(QUser.ACCOUNT, Condition.EQ, account);
        criteria.and(QUser.ID, Condition.NE, uid);
        Integer count = criteria.count();
        if (count > 0) {
            return -1;
        }
        user.setUpdateTime(new Date());
        criteria.update(user);
        //先清空用户对应角色, 在存储用户角色
        String roles = user.getRoles();
        saveUserRoles(uid, roles);

        //先清空用户对应组织, 在存储用户组织
        String orgs = user.getOrgs();
        saveUserOrgs(uid, orgs);
        return 1;
    }

    /**
     * 修改用户信息
     *
     * @param user 用户对象
     * @return
     */
    public int updateUserInfo(User user) {
        Long uid = user.getId();
        if (uid == null) {
            throw new TipException("用户ID不能为空!");
        }
        Criteria criteria = db.createCriteria(User.class);
        criteria.and(QUser.ID, Condition.EQ, uid);
        return criteria.update(user);
    }

    /**
     * 删除用户并且删除用户头像
     *
     * @param ids
     * @param filePath
     * @return
     */
    @Tx
    public int delete(Long[] ids, String filePath) {
        if (ids == null) {
            throw new TipException("ids 参数不能为空!");
        }
        int res = 0;
        Criteria&lt;User> criteria = db.createCriteria(User.class);
        criteria.label(QUser.AVATAR);
        for (Long id : ids) {
            //只查询用户头像信息
            User u = criteria.findById(id);
            //根据ID删除用户
            criteria.deleteById(id);
            String fileName = u.getAvatar();
            if (fileName != null && !"".equals(fileName)) {
                FileUtil.delete(filePath + File.separator + fileName);
            }
            res++;
        }
        return res;
    }

    public int checkLock(Long id, int lock) {
        User user = new User(id);
        user.setIsLock(lock);
        Criteria criteria = db.createCriteria(User.class);
        return criteria.update(user);
    }


    /**
     * 循环存储用户角色
     *
     * @param uid   用户ID
     * @param roles 角色字符串
     */
    private void saveUserRoles(Long uid, String roles) {
        if (roles == null) {
            return;
        }
        //清空用户对应角色
        Criteria criteria = db.createCriteria(UserRole.class);
        criteria.and(QUserRole.USER_ID, Condition.EQ, uid);
        criteria.delete();
        if ("".equals(roles.trim())) {
            return;
        }
        //循环存储用户角色
        for (String role : roles.split(",")) {
            UserRole userRole = new UserRole();
            userRole.setRoleId(Integer.parseInt(role));
            userRole.setUserId(uid);
            criteria.save(userRole);
        }
    }

    /**
     * 循环存储用户组织
     *
     * @param uid  用户ID
     * @param orgs 角色字符串
     */
    @Tx
    private void saveUserOrgs(Long uid, String orgs) {
        if (orgs == null) {
            return;
        }
        //清空用户对应组织
        Criteria criteria = db.createCriteria(UserOrg.class);
        criteria.and(QUserOrg.USER_ID, Condition.EQ, uid);
        criteria.delete();
        if ("".equals(orgs.trim())) {
            return;
        }
        //循环存储用户组织
        for (String org : orgs.split(",")) {
            UserOrg userOrg = new UserOrg();
            userOrg.setOrgId(Integer.parseInt(org));
            userOrg.setUserId(uid);
            criteria.save(userOrg);
        }
    }
}
</pre>
<script type="text/javascript">
    $(function () {
        SyntaxHighlighter.highlight();
        $(".toolbar").remove();
    });
</script>